package cn.edu.scau.cmi.huangxiyan.client;

import cn.edu.scau.cmi.huangxiyan.abstractFactory.*;
import cn.edu.scau.cmi.huangxiyan.abstractFactory.abstractMeat.Meat;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class AbstractFactoryClient extends Application {
	public AbstractFactory factory = null;
	public Meat meat = null;

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("AbstractFactoryClient");
		VBox vBox = new VBox(15);
		vBox.setPadding(new Insets(15, 5, 5, 5));

		TextField brandTextField = new TextField("请输入品牌（Wens、Tianbang或Aonong）");
		brandTextField.setPrefSize(500, 25);

		TextField meatTextField = new TextField("请输入肉的种类（pork、beef或chicken）");
		meatTextField.setPrefSize(500, 25);

		ChoiceBox<String> brandChoiceBox = new ChoiceBox<String>(
				FXCollections.observableArrayList("", "Wens", "Tianbang", "Aonong"));
		brandChoiceBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
			@SuppressWarnings("rawtypes")
			public void changed(ObservableValue ov, Number value, Number new_value) {
				String[] brand = { "", "Wens", "Tianbang", "Aonong" };
				brandTextField.setText(brand[new_value.intValue()]);
			};
		});
		brandChoiceBox.setPrefSize(100, 25);

		ChoiceBox<String> meatChoiceBox = new ChoiceBox<String>(
				FXCollections.observableArrayList("", "pork", "beef", "chicken"));
		meatChoiceBox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {
			@SuppressWarnings("rawtypes")
			public void changed(ObservableValue ov, Number value, Number new_value) {
				String[] meat = { "", "pork", "beef", "chicken" };
				meatTextField.setText(meat[new_value.intValue()]);
			};
		});
		meatChoiceBox.setPrefSize(100, 25);

		HBox brandHBox = new HBox(5);
		brandHBox.setPadding(new Insets(5, 0, 5, 0));
		brandHBox.getChildren().addAll(brandTextField, brandChoiceBox);

		HBox meatHBox = new HBox(5);
		meatHBox.setPadding(new Insets(5, 0, 5, 0));
		meatHBox.getChildren().addAll(meatTextField, meatChoiceBox);

		TextArea textArea = new TextArea("");
		textArea.setPrefSize(600, 200);
		textArea.setEditable(false);

		Button button = new Button("生产");
		button.setPrefSize(200, 25);
		button.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				String brand = brandTextField.getText();
				factory = null;
				switch (brand) {
				case "Wens":
					factory = new WensFactory();
					break;
				case "Tianbang":
					factory = new TianbangFactory();
					break;
				case "Aonong":
					factory = new AonongFactory();
					break;
				default:
					textArea.appendText("输入品牌错误，请重新输入Wens、Tianbang或Aonong！\n");
					brandTextField.setText("请输入品牌（Wens、Tianbang或Aonong）");
					break;
				}

				String meatType = meatTextField.getText();
				meat = null;

				if (factory != null) {
					switch (meatType) {
					case "pork":
						meat = factory.createPork();
						break;
					case "beef":
						meat = factory.createBeef();
						break;
					case "chicken":
						meat = factory.createChicken();
						break;
					default:
						textArea.appendText("输入肉的种类错误，请重新输入pork、beef或chicken！\n");
						meatTextField.setText("请输入肉的种类（pork、beef或chicken）");
					}
				} else {
					switch (meatType) {
					case "pork":
					case "beef":
					case "chicken":
						break;
					default:
						textArea.appendText("输入肉的种类错误，请重新输入pork、beef或chicken！\n");
						meatTextField.setText("请输入肉的种类（pork、beef或chicken）");
					}
				}

				if (factory != null && meat != null) {
					textArea.appendText(meat.cook() + "\n");
				}
			}
		});
		BorderPane borderPane = new BorderPane();
		borderPane.setCenter(button);

		vBox.getChildren().addAll(brandHBox, meatHBox, textArea, borderPane);
		Scene scene = new Scene(vBox, 600, 300);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}
